function [KA,Weights,name] = findRobustWeightsperfinal(VF,w,h,numparams,corrs)
%this function , given a camera center and a focal length and a series of fundamental
%matrices computes the error with respect to a fundamental matrix
%this function , given a camera center and a focal length and a series of fundamental
%matrices computes the error with respect to a fundamental matrix

cc=1;
for j=1:size(VF,2)
    if(size(VF{1,j},2)>0)
    TF{1,cc}=VF{1,j}/norm(VF{1,j},2);
    cc=cc+1;
    end
end

plotting=0;
[m,numFs]=size(TF);
name='some weird';

funcOption='regularweighted';
normalize=0;
numtries=1;


Weights=ones(numFs,1);
Weights=Weights/sum(Weights); % normalizing




kstart=[w 0 w/2; 0 w h/2; 0 0 1];



for i=1:4
    
    [allsols,  scrs, bestslns] =nonlinearOptimizeselfcalibnormMOD(TF, w,h, numparams ,Weights,numtries,kstart);
    
    for j=1:numFs
        G=(bestslns')*TF{1,j}*bestslns;
        s=svd(G);
        er=(s(1,1)-s(2,1));
        er2=(s(1,1)-s(2,1))/(s(1,1)+s(2,1));
        
        
        
        
        Ke= abs(kstart-bestslns);
        if(sum(sum(Ke))<0.001)
            break;
        end
        EM=(Ke')*TF{1,j}*bestslns+(bestslns')*TF{1,j}*Ke+(Ke')*TF{1,j}*Ke;
        thscales=2*(norm(EM,2));
        
        T2(j,1)=(er/thscales)^2;
        older(j,1)=er;
        
    end
    if(sum(sum(Ke))<0.001)
        break;
    end
    t2old=T2;
    T2=T2/sum(T2);
    if(sum(sum(isnan(T2)))>0)
        Weights
        T2
        display('error found');
    end
    
    
    
    
    r=T2;
    
    s=0.7413*iqr(r);
    r=r/(s);
    Weights2=exponfunc(r); % based on median of residuals
    
    
    Weights=Weights2;
    Weights=Weights/sum(Weights); % normalizing
    
    if(sum(sum(isnan(Weights)))>0)
        Weights
        T2
        display('error found in none');
    end
    kstart=bestslns;
end


KA=bestslns;
end

function w = exponfunc(r)
w = (exp(-r));
end